﻿using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO; 
using MyLib;
 
namespace PerformanceReport.AjaxPage
{
    /// <summary>
    /// GenerateReports 的摘要说明
    /// </summary>
    public class GenerateLoadsReports : IHttpHandler 
    {
        string strSql;
        string strJson;
        string strPara;
        string strStartDate;
        string strEndDate;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            strPara = context.Request["Para"];             //流程名
            strStartDate=context.Request["StartDate"];     //开始时间
            strEndDate = context.Request["EndDate"];       //结束时间

            strSql = "select Distinct Steplabel,ProcessName from Tasks";             
            string where = " where 1=1 ";
              where += " and ProcessName= '" + strPara.Trim() + "'";
            if (!string.IsNullOrEmpty(strStartDate))
            {
                where += " and  starttime >= to_date('" + strStartDate.Trim() + "','yyyy-MM-dd')";
            }
            if (!string.IsNullOrEmpty(strEndDate))
            {
                where += " and  starttime <= to_date('" + strEndDate.Trim() + "','yyyy-MM-dd')";
            }
            string StrSqlTotal = strSql + where;
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(StrSqlTotal.ToString());

            //如果存在返回值的情况
            if (dt.Rows.Count > 0)
            {
                DataTable dtTemp = new DataTable();
                dtTemp.Columns.Add("Steplabel");
                dtTemp.Columns.Add("ProcessName");
                DataRow NewRow = dtTemp.NewRow();
                NewRow["Steplabel"] = dt.Rows[0]["Steplabel"].ToString();
                NewRow["ProcessName"] = dt.Rows[0]["ProcessName"].ToString();
                dtTemp.Rows.Add(NewRow);

                string strProcessStep = InitLine(dt);
                string strFinishQty = TotalFinishQty(dt, strPara);
                string strUnFinishQty = TotalUnFinishQty(dt, strPara);

                strJson = "[";

                //循环所有的行
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                strJson += "{'ProcessStep':'" + CheckText(strProcessStep.Trim()) + "',"
                         + "'FinishQty':'" + CheckText(strFinishQty.Trim()) + "',"
                         + "'UnFinishQty':'" + CheckText(strUnFinishQty.Trim()) + "'},";
                //}
                strJson = strJson.Substring(0, strJson.LastIndexOf(','));
                strJson += "]";
            }

             context.Response.Buffer = true;
             context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
             context.Response.Expires = 0;
             context.Response.CacheControl = "no-cache";
             context.Response.Write(strJson);
        }

        //流程审批步骤
        public string InitLine(DataTable dt)
        {
            string strHtml = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strHtml += dt.Rows[i]["Steplabel"].ToString().Trim() + ",";
            }
            return strHtml;
            //string strValue = strSubstrng(strHtml);
            //return strValue;
        }

        //public string strSubstrng(string str)
        //{ 
        //    string [] strs=str.Split(',');
        //    string s = "";
        //    for(int i=0;i<strs.Length;i++)
        //    {
        //         if(s=="")
        //         {
        //             s+="'"+strs[i]+"'";
        //         }
        //         else 
        //         {
        //            s+=",'"+strs[i]+"'";
        //         }
        //    }
        //    return s;
        //}

        //完成数量
        public string TotalFinishQty(DataTable dt,string strProcessName)
        {
            string strHtmlCount = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strSql = "select Count(Steplabel) as Steplabel,ProcessName,Status from Tasks where ProcessName= '" + strProcessName.Trim() + "' and Steplabel='" + dt.Rows[i]["Steplabel"].ToString() + "' and Status=3  group by Steplabel,ProcessName,Status";
                DataTable dtCount = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
                if (dtCount.Rows.Count > 0)
                {
                    //strHtmlCount += "name:'完成数量'" + "," + "data:[" + dtCount.Rows[0]["Steplabel"].ToString().Trim() + "," + "]";
                    strHtmlCount += dtCount.Rows[0]["Steplabel"].ToString().Trim() + ",";
                }
                else
                {
                    strHtmlCount += "0" + ",";
                }
            }

            //拼接series值
            string strHeader = "{ type: 'column',name:'完成数量'" + "," + "data:" + "[";  
            string strEnd = "]}";
            string strTotal = strHeader + strHtmlCount.TrimEnd(',') + strEnd;           
            return strTotal;
            //return strHtmlCount.TrimEnd(',');
        }

        //待办数量
        public string TotalUnFinishQty(DataTable dt, string strProcessName)
        {
            string strHtmlCount = string.Empty;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strSql = "select Count(Steplabel) as Steplabel,ProcessName,Status from Tasks where ProcessName= '" + strProcessName.Trim() + "' and Steplabel='" + dt.Rows[i]["Steplabel"].ToString() + "' and Status=1  group by Steplabel,ProcessName,Status";
                DataTable dtCount = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
                if (dtCount.Rows.Count > 0)
                {
                    strHtmlCount += dtCount.Rows[0]["Steplabel"].ToString().Trim() + ",";
                }
                else
                {
                    strHtmlCount += "0" + ",";
                }
            }
            //拼接series值
            string strHeader = "{ type: 'column',name:'待办数量'" + "," + "data:" + "[";
            string strEnd = "]}";
            string strTotal = strHeader + strHtmlCount.TrimEnd(',') + strEnd;           
            return strTotal;
            //return strHtmlCount.TrimEnd(',');
        }

        public string CheckText(string Str)
        {
            string ReturnString = "";
            ReturnString = Str.Replace(",", "#dou#");
            ReturnString = Str.Replace("'", "#dan#");
            return ReturnString;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
         
    }
}